package com.ueip.backend.controller;

import com.ueip.backend.service.ClueService;
import com.ueip.backend.vo.Result;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/api/clue")
public class ClueController {
    @Resource
    private ClueService clueService;

    // 添加线索
    @PostMapping("/add")
    Result add(@RequestBody Map<String, String> data) {
        return clueService.add(data);
    }

    // 更新线索
    @PostMapping("/update")
    Result update(@RequestBody Map<String, String> data) {
        return clueService.update(data);
    }

    // 通过线索id删除线索
    @PostMapping("/delete/by/id")
    Result deleteById(@RequestBody Map<String, String> data) {
        return clueService.deleteById(data);
    }

    // 获取所有线索
    @PostMapping("/get/all")
    Result getAll() {
        return clueService.getAll();
    }

    // 通过条件构造器进行查询，不含分页
    @PostMapping("/get/clue/by/condition")
    Result getClueByConditions(@RequestBody Map<String, String > data) {
        return clueService.getClueByConditions(data);
    }

    // 这里通过xml文件中的sql语句机型查询，含有分页
    @PostMapping("/get/clue/by/condition2")
    Result getClueByConditions2(@RequestBody Map<String, String > data) {
        return clueService.getClueByConditions2(data);
    }

    // 通过条件进行查询，并进行计数count
    @PostMapping("/get/clue/by/condition2/num")
    Result getClueByConditions2Num(@RequestBody Map<String, String> data) {
        return clueService.getClueByConditions2Num(data);
    }

    // 设置报道
    @PostMapping("/set/reported")
    Result setReported(@RequestBody Map<String, String> data) {
        return clueService.setReported(data);
    }

    // 取消报道
    @PostMapping("/unset/reported")
    Result unsetReported(@RequestBody Map<String, String> data) { return clueService.unsetReported(data); }

    // 统计，接受多条件
    @PostMapping("/statistics/by/conditions")
    Result statisticsByConditions(@RequestBody Map<String, String> data) {
        return clueService.statisticsByConditions(data);
    }

    // 生成统计信息
    @PostMapping("/generate/clue/statistic")
    Result generateProvCluesStatistic(@RequestBody Map<String, String> data) {
        return clueService.generateProvCluesStatistic2(data);
    }

    // 设置通知书
    @PostMapping("/notification/add")
    Result addNotification(@RequestBody Map<String, String> data) {
        return clueService.addNotification(data);
    }

    // 删除通知书
    @PostMapping("/notification/delete/by/id")
    Result notificationDeleteById(@RequestBody Map<String, String> data) {
        return clueService.notificationDeleteById(data);
    }

    // 通过条件查询通知书
    @PostMapping("/notification/select/by/condition")
    Result notificationSelectByCondition(@RequestBody Map<String, String> data) {
        return clueService.notificationSelectByCondition(data);
    }

    // 生成excel文件
    @PostMapping("/generate/excel/by/condition")
    void exportClueDataToExcel(@RequestBody Map<String, String> data, HttpServletResponse response) throws IOException {
        XSSFWorkbook workbook = (XSSFWorkbook) clueService.generateExcel(data).getData();

        FileOutputStream fos = new FileOutputStream("output.xlsx");
        workbook.write(fos);
        fos.close();
        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=output.xlsx");

        // 将输出流写入响应
        try (FileInputStream inputStream = new FileInputStream("output.xlsx")) {
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                response.getOutputStream().write(buffer, 0, bytesRead);
            }
            response.flushBuffer();
        }

        // 删除临时文件
        File tempFile = new File("output.xlsx");
        if (tempFile.exists()) {
            tempFile.delete();
        }
    }
}
